/* global window */
import modelExtend from 'dva-model-extend'
import { list, query, create, update, remove, patchremove, getPermission, set } from 'services/roles'
import { message } from 'antd'
import { getMenuTree } from 'services/menus'
import { pageModel } from './common'
import queryString from 'query-string'

export default modelExtend(pageModel, {
  namespace: 'roles',

  state: {
    // currentItem: {},
    // modalVisible: false,
    // modalType: 'create',
    // selectedRowKeys: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/roles') {
          const payload = queryString.parse(location.search) || { page: 1 }
          dispatch({
            type: 'list',
            payload,
          })
          dispatch({
            type: 'getMenuTree',
          })
        }
      })
    },
  },

  effects: {
    * list ({ payload = {} }, { call, put }) {
      const data = yield call(list, payload)
      const listData = data.data
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
    },
    * getMenuTree ({}, { call, put }) {
      const response = yield call(getMenuTree)
      if (response.code === 0) {
        yield put({
          type: 'updateState',
          payload: {
            menuTreeData: response.data,
          },
        })
      }
    },
    * delete ({ payload }, { call, put, select }) {
      const data = yield call(remove, { id: payload })
      // const { selectedRowKeys } = yield select(_ => _.basicData)
      if (data.success) {
        message.success('delete success')
        // yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'list' })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * multiDelete ({ payload }, { call, put }) {
      const data = yield call(patchremove, payload)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        yield put({ type: 'list' })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * create ({ payload }, { call, put }) {
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('create failed')
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const id = yield select(({ roles }) => roles.currentItem.id)
      const newData = { ...payload, id }
      const data = yield call(update, newData)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('update failed')
        throw data
      }
    },

    * showModal ({ payload }, { call, put }) {
      let sendData = {}
      sendData.id = payload.currentItem.id
      const data = yield call(query, sendData)
      if (data.success) {
        yield put({
          type: 'updateModalState',
          payload: {
            modalType: 'update',
            currentItem: data.data,
            modalVisible: true,
          },
        })
      } else {
        throw data
      }
    },
    * showPermissionModal ({ payload }, { call, put }) {
      const response = yield call(getPermission, { roleId: payload.roleId.toString() })
      // if (response.message) {
      yield put({
        type: 'updatePermissionModalState',
        payload: {
          modalType: payload.modalType,
          permissionModalVisible: true,
          currentPermission: response.data,
        },
      })
      // }
    },
    * updatePermission ({ payload }, { call, put }) {
      console.log(payload)
      const response = yield call(set, payload)
      if (response.success) {
        message.success('setPermission success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('setPermission failed')
        throw response
      }
    },

  },

  reducers: {

    // updateSelectState (state, { payload }) {
    //   return { ...state, ...payload }
    // },
    //
    // updateModalState (state, { payload }) {
    //   return { ...state, ...payload, modalVisible: true }
    // },
    //
    // hideModal (state) {
    //   return { ...state, modalVisible: false }
    // },
  },
})
